//
//  ST_GuardHerAlertController.swift
//  btvideo
//
//  Created by mac on 2020/10/21.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes
import SwiftyJSON

class ST_GuardHerAlertController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.height.equalTo(456)
            make.center.equalToSuperview()
        }
        
        root_view.addSubview(topGradientView)
        topGradientView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(207)
        }
        
        root_view.addSubview(left_image_view)
        root_view.addSubview(right_image_view)
        root_view.addSubview(heart_image_view)
        
        
        heart_image_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(40)
            make.height.width.equalTo(46)
        }
        
        left_image_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(-35)
            make.width.height.equalTo(86)
            make.centerY.equalTo(heart_image_view)
        }
        
        right_image_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(35)
            make.width.height.equalTo(86)
            make.centerY.equalTo(heart_image_view)
        }
        
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(25)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(126)
        }
        
        root_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(157)
            make.left.equalToSuperview().offset(25)
            make.right.equalToSuperview().offset(-25)
        }
        
        root_view.addSubview(mediumTextView)
        mediumTextView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(215)
            make.centerX.equalToSuperview()
            make.width.equalTo(247)
            make.height.equalTo(120)
        }
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.height.equalTo(44)
            make.width.equalTo(200)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-52)
        }
        
        root_view.addSubview(cancel_btn)
        cancel_btn.snp.makeConstraints { (make) in
            make.height.equalTo(44)
            make.width.equalTo(200)
            make.centerX.equalToSuperview()
            make.top.equalTo(confirm_btn.snp.bottom)
        }
        
        bind_data()
    }
    
    fileprivate func bind_data(){
        
        guard let m = dataModel,let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo else { return }
        
        
        right_image_view.pin_setImage(from: user.avatar.compress_small_image(width: 86*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        left_image_view.pin_setImage(from: m.herAvatar.compress_small_image(width: 86*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        let normal_attribute = [Attribute.font(Font.systemFont(ofSize: 13, weight: UIFont.Weight.regular)),Attribute.textColor(UIColor.white)]
        
        let p = NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 3)
        
        p.alignment = .center
        
        let paragraphAttribute = [Attribute.paragraphStyle(p)]
        
//        有人守护中
        if m.guardTime.double > Date().timeIntervalSince1970{
            
            titleLabel.text = "取代他的甜蜜守护"
            
            
            
            let highlineAttribute = [Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.medium)),Attribute.textColor(UIColor.init(hex: 0xED3E58)!)]
            
            let str = "赠送至少".withAttributes(normal_attribute)
            
            str.append("\(m.guardNextCoin)甜心币".withAttributes(highlineAttribute))
            
            str.append("的守护即可取而代之，成为她的唯一甜蜜守护。".withAttributes(normal_attribute))
            
            str.addAttributes(paragraphAttribute, range: NSRange.init(location: 0, length: str.length))
            
            content_label.attributedText = str
            
            confirm_btn.setTitleForAllStates("我要取代他")
            
            cancel_btn.setTitleForAllStates("不是对手，放弃")
            
        }else{
            
            titleLabel.text = "成为唯一守护帮你人工牵线"
            
            let str =  "喜欢不用等，成为唯一守护！红娘立刻帮你认识！线下真实见面机会增加10倍！".withAttributes(normal_attribute)
            
            str.addAttributes(paragraphAttribute, range: NSRange.init(location: 0, length: str.length))
            
            content_label.attributedText = str
            
            confirm_btn.setTitleForAllStates("立即守护她")
            
            cancel_btn.setTitleForAllStates("稍后再说")
        }
        
         
        
        
    }
    

// MARK: - Properties
    var dataModel:PairsGuardModel?
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.white
        control.cornerRadius = 15
        return control
    }()

    
    fileprivate lazy var heart_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("gurdeSuccessIcon")
        return control
    }()
    
    fileprivate lazy var topGradientView:ST_FWGradientView = {
       
        let control = ST_FWGradientView()
        
        control.bgLayer.colors = [UIColor.init(hex: 0xEDC26A)!.cgColor,UIColor.init(hex: 0xF1D6A0)!.cgColor,UIColor.init(hex: 0xEDC26A)!.cgColor]
        control.bgLayer.locations = [0,0.5,1]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        
        return control
    }()
   
    fileprivate lazy var left_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
//        control.backgroundColor = .random
        control.cornerRadius = 86/2.0
        return control
    }()
    
    fileprivate lazy var right_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
//        control.backgroundColor = .random
        control.cornerRadius = 86/2.0
        return control
    }()
    
    fileprivate lazy var mediumTextView:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("gurdeMediumText")!)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        return control
    }()
    
    
    
    fileprivate lazy var confirm_btn:ST_BTGradientButton = {
            
        let control = ST_BTGradientButton()
        control.bgLayer.colors = [UIColor.init(hex: 0xF1D6A0)!.cgColor,UIColor.init(hex: 0xEDC26A)!.cgColor]
        control.cornerRadius = 44/2.0
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var cancel_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
            control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.setTitleColor(UIColor.init(hex: 0x61666F), for: UIControl.State.normal)
//      control.semanticContentAttribute = .forceRightToLeft
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    var sendGiftClosure:(()->())?
    
    @objc fileprivate func confirm_handle(){
        
        goback_previous_controller(animated: false) {
            
            self.sendGiftClosure?()
        }
        
    }
    
    @objc fileprivate func cancel_handle(){
        
        goback_previous_controller(animated: false)
    }
}



//MARK: - Pairs Guard Model
class PairsGuardModel: NSObject {
    
    var herAvatar = ""
    
    var guardMemberAvatar = ""
    
    var guardTime = 0
    
    var isMaxGuard = false
    
    var guardNextCoin = 0
    
    var guardMemberAccid = ""
    
    var guardDay = 0
    
    init(json:JSON,avatar:String) {
        
        super.init()
        
        self.herAvatar = avatar
        
        guardMemberAvatar = json["guard_member_avatar"].stringValue
        
        guardTime = json["guard_time"].intValue
        
        isMaxGuard = json["guard_is_max"].boolValue
        
        guardNextCoin = json["guard_next_coin"].intValue
        
        guardMemberAccid = json["guard_member_accid"].stringValue
        
        guardDay = json["guard_day"].intValue
        
        
    }
    
    override init() {
        super.init()
    }
    
    class func create_present_finish_model(notificationJson:JSON,avatar:String)->PairsGuardModel{
        
        
        let m = PairsGuardModel()
        
        m.herAvatar = avatar
        
        if let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo {
            m.guardMemberAvatar = user.avatar
            m.guardMemberAccid = user.accid
        }
        
        m.guardTime = notificationJson["guardTime"].intValue
        
        m.isMaxGuard = notificationJson["guard_is_max"].boolValue
        
        m.guardNextCoin = notificationJson["guard_next_coin"].intValue
        
        m.guardDay = notificationJson["effectiveTime"].intValue
        
        return m
    }
    
}




class GuardHerMaxAlertController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.height.equalTo(320)
            make.center.equalToSuperview()
        }
        
        root_view.addSubview(guideMemberView)
        
        guideMemberView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.width.height.equalTo(120)
        }
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(25)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(160)
        }
        
        root_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(191)
            make.left.equalToSuperview().offset(35)
            make.right.equalToSuperview().offset(-35)
        }
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.height.equalTo(44)
            make.width.equalTo(200)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }
        bind_data()
    }
    
    fileprivate func bind_data(){
        
        guard let m = dataModel else{return}
        
        guideMemberView.bind(title: m.guardDay.string + "天守护", avatar: m.guardMemberAvatar)
        
    }
    
    
    // MARK: - Properties
    var dataModel:PairsGuardModel?
    
    var info:(title:String,content:String)?{
        didSet{
            
            guard let v = info else{return}
            
            titleLabel.text = v.title
            
            let normal_attribute = [Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.regular)),Attribute.textColor(UIColor.init(hex: 0x61666F)!)]
            
            let p = NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 3)
            
            p.alignment = .center
            
            let paragraphAttribute = [Attribute.paragraphStyle(p)]
      
            let str =  v.content.withAttributes(normal_attribute)
            
            str.addAttributes(paragraphAttribute, range: NSRange.init(location: 0, length: str.length))
            
            content_label.attributedText = str
        }
    }
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.white
        control.cornerRadius = 15
        return control
    }()
    
    fileprivate lazy var guideMemberView:GuardHerControl = {
        let control = GuardHerControl()
          return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        
        let control = UILabel.init()
        
        control.numberOfLines = 0
        
        
        
        return control
    }()
    
    fileprivate lazy var confirm_btn:ST_BTGradientButton = {
            
        let control = ST_BTGradientButton()
        control.setTitleForAllStates("好的")
        control.bgLayer.colors = [UIColor.init(hex: 0xF1D6A0)!.cgColor,UIColor.init(hex: 0xEDC26A)!.cgColor]
        control.cornerRadius = 44/2.0
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    @objc fileprivate func confirm_handle(){
        
        
        goback_previous_controller(animated: false)
    }
}



class GuardHerControl: UIControl {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(avatar_image_view)
        self.addSubview(avatarOfGuideBackgroundImageView)
        
        avatar_image_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
            make.width.height.equalTo(90)
        }
        
        avatarOfGuideBackgroundImageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.width.height.equalTo(120)
        }
        
        avatarOfGuideBackgroundImageView.addSubview(titleGuideLabel)
        titleGuideLabel.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-18)
            make.centerX.equalToSuperview()
            make.height.equalTo(17)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var avatarOfGuideBackgroundImageView:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("守护人的高级框")!)
          return control
    }()
    
    
    fileprivate lazy var titleGuideLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hexString: "#BB3226")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.semibold)
        return control
    }()
        
    fileprivate lazy var avatar_image_view:UIImageView = {
         let control = UIImageView()
           control.contentMode = .scaleAspectFill
            control.cornerRadius = 45
          return control
      }()
        
    
    func bind(title:String,avatar:String) {
        
        titleGuideLabel.text = title
        
        avatar_image_view.pin_setImage(from: avatar.compress_small_image(width: 90*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
    }
    
}


class GuardHerSmallControl: UIControl {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(avatar_image_view)
        self.addSubview(avatarOfGuideBackgroundImageView)
        
        avatar_image_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(12)
            make.width.height.equalTo(76)
        }
        
        avatarOfGuideBackgroundImageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.width.height.equalTo(100)
        }
        
        avatarOfGuideBackgroundImageView.addSubview(titleGuideLabel)
        titleGuideLabel.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-14)
            make.centerX.equalToSuperview()
            make.height.equalTo(16)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var avatarOfGuideBackgroundImageView:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("守护人的高级框")!)
          return control
    }()
    
    
    fileprivate lazy var titleGuideLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hexString: "#BB3226")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.semibold)
        return control
    }()
        
    fileprivate lazy var avatar_image_view:UIImageView = {
         let control = UIImageView()
           control.contentMode = .scaleAspectFill
        control.cornerRadius = 76/2.0
          return control
      }()
        
    
    func bind(title:String,avatar:String) {
        
        titleGuideLabel.text = title
        
        avatar_image_view.pin_setImage(from: avatar.compress_small_image(width: 100*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
    }
    
}
